#include "../mqserver/binding.hpp"

#include <gtest/gtest.h>

mymq::BindingManager::ptr bmp;

class BindingTest: public testing::Environment
{
    virtual void SetUp() override
    {
        bmp = std::make_shared<mymq::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // mqmp->clear();
        std::cout << "完成资源的清理化工作" << std::endl;
    }
};

TEST(binding_test, insert_test)
{
    bmp->Bind("exchange1", "queue1", "news.music.#", true);
    bmp->Bind("exchange1", "queue2", "news.sport.#", true);
    bmp->Bind("exchange2", "queue1", "news.music.pop", true);
    bmp->Bind("exchange2", "queue2", "news.sport.football", true);
    ASSERT_EQ(bmp->Size(), 4);
}

TEST(binding_test, select_test)
{
    ASSERT_EQ(bmp->Exist("exchange1", "queue1"), true);
    ASSERT_EQ(bmp->Exist("exchange1", "queue2"), true);
    mymq::Binding::ptr bp = bmp->GetBinding("exchange1", "queue2");
    ASSERT_NE(bp.get(), nullptr);
    ASSERT_EQ(bp->exchange_name, "exchange1");
    ASSERT_EQ(bp->msgqueue_name, "queue2");
    ASSERT_EQ(bp->binding_key, "news.sport.#");
    ASSERT_EQ(bp->durable, true);

    mymq::BindingMapper::MsgQueueBindingMap mqmap = bmp->GetExchangeBinding("exchange2");
    ASSERT_EQ(mqmap.size(), 2);
    ASSERT_NE(mqmap.find("queue1"), mqmap.end());
    ASSERT_NE(mqmap.find("queue2"), mqmap.end());
}

TEST(binding_test, delete_test)
{
    bmp->UnBind("exchange1", "queue1");
    ASSERT_EQ(bmp->Exist("exchange1", "queue1"), false);
    bmp->RemoveExchangeBindings("exchange1");
    ASSERT_EQ(bmp->Exist("exchange1", "queue2"), false);
    bmp->RemoveMsgQueueBindings("queue1");
    ASSERT_EQ(bmp->Size(), 1);
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();

    return 0;
}